﻿// ****************************************************************
// Copyright 2012, Stephan Burguchev
// e-mail: me@sburg.net
// ****************************************************************
// *
using System;
using System.Collections.Generic;
using NUnit.Framework;
using Updater.Common;
using Updater.Updates;
using Version = Updater.Utils.Version;

namespace Updater.Tests
{
    [TestFixture]
    public class AlgorithmsTests
    {
        private string _v1;
        private string _v10;
        private string _v2;
        private string _v3;
        private string _v4;
        private string _v5;
        private string _v6;
        private string _v7;
        private string _v8;
        private string _v9;

        [Test]
        public void GetShortestUpdateSequence_AlternativeWays()
        {
            // v
            // 1 2 3 4 5 6 7 8 9 10
            // ---------
            //       -----------
            //                 ----
            //     ----------------
            var updates = new[]
                {
                    this.createUpdateInfo(this._v1, this._v5),
                    this.createUpdateInfo(this._v4, this._v9),
                    this.createUpdateInfo(this._v9, this._v10),
                    this.createUpdateInfo(this._v3, this._v10),
                };

            List<Version> versionsChain;
            List<UpdateInformation> result = Algorithms.GetShortestUpdateSequence(
                updates,
                Version.Parse(this._v1),
                out versionsChain);

            Assert.AreEqual(Version.Parse(this._v1), versionsChain[0]);
            Assert.AreEqual(Version.Parse(this._v5), versionsChain[1]);
            Assert.AreEqual(Version.Parse(this._v10), versionsChain[2]);
        }

        [Test]
        public void GetShortestUpdateSequence_OverlappingSet()
        {
            //     v
            // 1 2 3 4 5 6 7 8 9 10
            // ---------
            //       -----------
            //                 ----
            var updates = new[]
                {
                    this.createUpdateInfo(this._v1, this._v5),
                    this.createUpdateInfo(this._v4, this._v9),
                    this.createUpdateInfo(this._v9, this._v10),
                };

            List<Version> versionsChain;
            List<UpdateInformation> result = Algorithms.GetShortestUpdateSequence(
                updates,
                Version.Parse(this._v3),
                out versionsChain);

            Assert.AreEqual(Version.Parse(this._v3), versionsChain[0]);
            Assert.AreEqual(Version.Parse(this._v5), versionsChain[1]);
            Assert.AreEqual(Version.Parse(this._v9), versionsChain[2]);
            Assert.AreEqual(Version.Parse(this._v10), versionsChain[3]);
        }

        [Test]
        public void GetShortestUpdateSequence_Simple()
        {
            // v
            // 1 2 3 4 5 6 7 8 9 10
            // --------------------
            var updates = new[]
                {
                    this.createUpdateInfo(this._v1, this._v10),
                };

            List<Version> versionsChain;
            List<UpdateInformation> result = Algorithms.GetShortestUpdateSequence(
                updates,
                Version.Parse(this._v1),
                out versionsChain);

            Assert.AreEqual(Version.Parse(this._v1), versionsChain[0]);
            Assert.AreEqual(Version.Parse(this._v10), versionsChain[1]);
        }

        [TestFixtureSetUp]
        public void SetUp()
        {
            this._v1 = "1.0.0.0";
            this._v2 = "2.0.0.0";
            this._v3 = "3.0.0.0";
            this._v4 = "4.0.0.0";
            this._v5 = "5.0.0.0";
            this._v6 = "6.0.0.0";
            this._v7 = "7.0.0.0";
            this._v8 = "8.0.0.0";
            this._v9 = "9.0.0.0";
            this._v10 = "10.0.0.0";
        }

        private UpdateInformation createUpdateInfo(string minVersion, string version)
        {
            return new UpdateInformation(
                version,
                version,
                minVersion == null
                    ? null
                    : Version.Parse(minVersion),
                Version.Parse(version),
                DateTime.Now,
                new Uri("http://www.ya.ru"),
                0,
                typeof(ZipArchiveUpdate),
                new byte[0],
                ChecksumType.Sha1);
        }
    }
}
